import os
import sys

UP = '1'
DOWN = '4'
LOG = '2'
OUT = '3'

def convert(month, day, time):
    day = int(day)
    if month == 'Sep':
        day += 31
    day -= 10
    h, m, s = [ int(x) for x in time.split(':')]
    time = day * 24 * 60
    time += h * 60
    time += m
    if time < 0:
        print 'ERROR!', month, day, time
        import traceback
        traceback.print_stack()
    return time


class State:
    def __init__(self, time, state):
        self.time = time
        self.state = state
    def __repr__(self):
        return str(self.time) + " " + self.state
    def __cmp__(self, other):
        return 10 * (self.time - other.time) + cmp(self.state, other.state)

def filter(month, day, time):
    f = (month not in ['Aug', 'Sep']) or (month == 'Aug' and int(day) < 10)
    return f

def parse_auth(f):
    m_login = 'pam_unix(gdm:session): session opened'
    m_logout = 'pam_unix(gdm:session): session closed'
    result = []
    for a in open(f):
        month, day, time = a.split()[0:3]
        if filter(month, day, time):
            continue
        if m_login in a:
            result.append(State(convert(month, day, time), LOG))
        if m_logout in a:
            state = State(convert(month, day, time), OUT)
            if result and result[-1].time == state.time:
                result.pop()
            else:
                result.append(state)
    return result

def parse_msg(cache, f):
    reading_boot = False
    result = []
    for a in open(f):
        month, day, time = a.split()[0:3]
        if filter(month, day, time):
            continue
        if '0.000000' in a:
            if reading_boot:
                continue
            else:
                reading_boot = True
            for b in reversed(cache):
                l_month, l_day, l_time = b.split()[0:3]
                if time != l_time or day != l_day or month != l_month:
                    u_state = State(convert(l_month, l_day, l_time), DOWN)
                    d_state = State(convert(month, day, time), UP)
                    if d_state.time != u_state.time:
                        result.append(d_state)
                        result.append(u_state)
                    break
            cache = []
        else:
            reading_boot = False
            cache.append(a)
    return result


dir = sys.argv[1]

files = [ 'auth.log.' + str(x) for x in range(6, -1, -1) ] + [ 'auth.log' ]
result = []
for a in files:
    try:
        result += parse_auth(dir + os.sep + a)
    except Exception, e:
        continue

files = [ 'messages.' + str(x) for x in range(6, -1, -1) ] + [ 'messages' ]
for a in files:
    cache = []
    try:
        result += parse_msg(cache, dir + os.sep + a)
    except Exception, e:
        continue

result.sort()
for a in result:
    print a
